﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Entity;

namespace DAL
{
    public class ConfigDAL:SqlDataProvider
    {
        #region[Config_GetByAll]
        public List<Config> Config_GetByAll(string Lang)
        {
            var list = new List<Config>();
            using (SqlCommand cmd=new SqlCommand("sp_Config_GetByAll",GetConnection()))
            {
                cmd.CommandType=CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                using (SqlDataReader dr=cmd.ExecuteReader())
                {
                    if(dr.HasRows)
                        while (dr.Read())
                        {
                            list.Add(Config.ConfigIDataReader(dr));
                        }
                }


            }
            return list;
        }
        #endregion
        #region[Config_GetById]
        public List<Config> Config_GetById(string Id)
        {
            var list = new List<Config>();
            using (SqlCommand cmd=new SqlCommand("sp_Config_GetById",GetConnection()))
            {
                cmd.CommandType=CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Id", Id));
                using(SqlDataReader dr=cmd.ExecuteReader())
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            list.Add(Config.ConfigIDataReader(dr));

                        }
                    }
                }
            }
            return list;
        }
        #endregion
        #region[Config_GetByTop]
        public List<Config> Config_GetByTop(string Top,string Where,string Order)
        {
            var list = new List<Config>();
            using (SqlCommand cmd=new SqlCommand("sp_Config_GetByTop",GetConnection()))
            {
                cmd.CommandType=CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Top", Top));
                cmd.Parameters.Add(new SqlParameter("@Where", Where));
                cmd.Parameters.Add(new SqlParameter("@Order", Order));
                SqlDataReader dr = cmd.ExecuteReader();
                if(dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Config.ConfigIDataReader(dr));
                    }
                    dr.Close();
                }
                return list;

            }
        }
        #endregion
        #region[Config_Insert]
        public bool Config_Insert(Config data)
        {
            using (SqlCommand cmd=new SqlCommand("sp_Config_Insert",GetConnection()))
            {
                cmd.Parameters.Add(new SqlParameter("@Mail_Smtp",data.Mail_Smtp));
                cmd.Parameters.Add(new SqlParameter("@Mail_Port",data.Mail_Port));
                cmd.Parameters.Add(new SqlParameter("@Mail_Info", data.Mail_Info));
                cmd.Parameters.Add(new SqlParameter("@Mail_Noreply", data.Mail_Noreply));
                cmd.Parameters.Add(new SqlParameter("@Mail_Password", data.Mail_Password));
                cmd.Parameters.Add(new SqlParameter("@PlaceHead", data.PlaceHead));
                cmd.Parameters.Add(new SqlParameter("@PlaceBody", data.PlaceBody));
                cmd.Parameters.Add(new SqlParameter("@GoogleId", data.GoogleId));
                cmd.Parameters.Add(new SqlParameter("@Contact", data.Contact));
                cmd.Parameters.Add(new SqlParameter("@Copyright", data.Copyright));
                cmd.Parameters.Add(new SqlParameter("@Title", data.Title));
                cmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                cmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                cmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                cmd.ExecuteNonQuery();
            }
            System.Web.HttpContext.Current.Cache.Remove("Config");

            return true;
        }

        #endregion
        #region[Config_Paging]
        public List<Config> Config_Paging(string CurrentPage,string PageSize,string Lang)
        {
            var list = new List<Config>();
            using (SqlCommand cmd=new SqlCommand("sp_Config_Paging",GetConnection()))
            {
                cmd.CommandType=CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CurrentPage", CurrentPage));
                cmd.Parameters.Add(new SqlParameter("@PageSize", PageSize));
                cmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                using(SqlDataReader dr=cmd.ExecuteReader())
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            list.Add(Config.ConfigIDataReader(dr));

                        }
                    }
                }
            }
            return list;
        }
        #endregion
        #region[Config_Update]
        public bool Config_Update(Config data)
        {
            using (SqlCommand cmd = new SqlCommand("sp_Config_Update", GetConnection()))
            {
                cmd.Parameters.Add(new SqlParameter("@Id", data.Id));
                cmd.Parameters.Add(new SqlParameter("@Mail_Smtp", data.Mail_Smtp));
                cmd.Parameters.Add(new SqlParameter("@Mail_Port", data.Mail_Port));
                cmd.Parameters.Add(new SqlParameter("@Mail_Info", data.Mail_Info));
                cmd.Parameters.Add(new SqlParameter("@Mail_Noreply", data.Mail_Noreply));
                cmd.Parameters.Add(new SqlParameter("@Mail_Password", data.Mail_Password));
                cmd.Parameters.Add(new SqlParameter("@PlaceHead", data.PlaceHead));
                cmd.Parameters.Add(new SqlParameter("@PlaceBody", data.PlaceBody));
                cmd.Parameters.Add(new SqlParameter("@GoogleId", data.GoogleId));
                cmd.Parameters.Add(new SqlParameter("@Contact", data.Contact));
                cmd.Parameters.Add(new SqlParameter("@Copyright", data.Copyright));
                cmd.Parameters.Add(new SqlParameter("@Title", data.Title));
                cmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                cmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                cmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                cmd.ExecuteNonQuery();
            }
            System.Web.HttpContext.Current.Cache.Remove("Config");

            return true;
        }
        #endregion
        #region[Config_Delete]
        public bool Config_Delete(string Id)
        {
            using (SqlCommand cmd=new SqlCommand("sp_Config_Delete",GetConnection()))
            {
                cmd.CommandType=CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Id", Id));
                cmd.ExecuteNonQuery();
            }
            System.Web.HttpContext.Current.Cache.Remove("Config");
            return true;
        }
        #endregion
    }
}
